﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;


namespace StudentWeb.Api.Controllers
{
    public class CourseController : ApiController
    {

        CourseBO _courseBO = new CourseBO();
        #region 课程信息 分页列表
        /// <summary>
        /// 图书信息 分页列表
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidToken]
        public IHttpActionResult GetCourseList(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                int pageIndex = model.page_index ?? 0;
                int pageSize = model.page_size ?? 0;
                if (pageIndex < 1 || pageSize < 1) return Json(new Result(ResultCode.Fail, "参数错误"));
                var wc = _GetCourseListWhereC(model.condidate);
                var result = _courseBO.GetCourseList(pageIndex, pageSize, wc);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        private string _GetCourseListWhereC(dynamic qc)
        {
            var wc = "CourseDataID>0";
            if (qc == null) return wc;
            string tmp = "";
            tmp = ConvertHelper.DynamicToString(qc.tabkey);
            if (!string.IsNullOrEmpty(tmp) && tmp != "all") { wc += $" and (CourseState={tmp})"; }
            tmp = ConvertHelper.DynamicToString(qc.courseName);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and CourseName like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.courseCredit);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and CourseCredit like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.courseNo);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and CourseNo like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.courseTerm);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and CourseTerm like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.courseInfo);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and CourseInfo like '%{tmp}%'"; }
            return wc;
        }
        #endregion
        #region 课程信息 单条记录
        /// <summary>
        /// 图书信息 单条记录
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        [ValidToken]
        public IHttpActionResult GetCourseInfo(string token, int courseDataID)
        {
            try
            {
                var result = _courseBO.GetCourseInfo(courseDataID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 课程信息 保存
        /// <summary>
        /// 学生信息 保存
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidToken]
        public IHttpActionResult SaveCourseInfo(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var user = new TeacherBO().GetTeacherInfo(token);
                var result = _courseBO.SaveCourseInfo(model, user.TeacherDataID);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 课程信息 状态

        /// <summary>
        /// 图书信息 状态
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidToken]
        public IHttpActionResult SetCourseState(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            int courseDataID = model.id;
            int courseState = model.state;
            try
            {
                var user = new TeacherBO().GetTeacherInfo(token);
                var result = _courseBO.SetCourseState(courseDataID, courseState);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
    }

}


